/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package taglibs.admin;

import daos.MemberDAO;
import daos.OrderDAO;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import models.Member;
import models.Order;
import util.Common;
import util.WebConst;

/**
 *
 * @author trietto
 */
public class OrderFormEdit extends TagSupport {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    /*
     * (non-Javadoc)
     * @see javax.servlet.jsp.tagext.TagSupport#doStartTag()
     */
    public int doStartTag() {
        try {
            JspWriter writer = pageContext.getOut();
            String actionName = pageContext.getRequest().getParameter(WebConst.ACTION);

            ArrayList<String> lstMemberData = new ArrayList<String>();
            ArrayList<Member> lstMember = MemberDAO.singleton().selectAll();
            for (Member member : lstMember) {
                lstMemberData.add(member.getMemberLogin() + "\t" + member.getMemberId());
            }

            if (actionName.equalsIgnoreCase(WebConst.EDIT)) {
                String orderId = pageContext.getRequest().getParameter("orderid");
                Order order = OrderDAO.singleton().select(orderId);
                if (order == null && actionName.equalsIgnoreCase(WebConst.EDIT)) {
                    return SKIP_BODY;
                }
                SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
                writer.println("<form action='AdminOrderServlet?action=update&orderid=" + orderId + "' method='POST'>");
                writer.println("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
                writer.println("<tr>");
                writer.println("<td>Member Id:</td>");
                writer.println("<td>" + Common.createTextbox("txtMemberId", "txtMemberId", "" + order.getMemberId(), "readonly=readonly", "") + "</td>");
                writer.println("</tr>");
                writer.println("<td>Order Date:</td>");
                writer.println("<td>" + Common.createTextbox("txtOrderDate", "txtOrderDate", sdf.format(order.getOrderDate()), "", "") + "</td>");
                writer.println("</tr>");
                writer.println("<td>Payment:</td>");
                writer.println("<td>" + Common.createTextbox("txtPayment", "txtPayment", "" + order.getPayment(), "", "") + "</td>");
                writer.println("</tr>");
                writer.println("<td>Shipping:</td>");
                writer.println("<td>" + Common.createTextbox("txtShipping", "txtShipping", "" + order.getShipping(), "", "") + "</td>");
                writer.println("</tr>");
                writer.println("<tr colspan='2'>");
                writer.println("<td>" + Common.createSubmitButton("btnUpdate", "btnUpdate", "Update", "") + "</td>");
                writer.println("</tr>");
                writer.println("</table>");
                writer.println("</form>");
            } else if (actionName.equalsIgnoreCase(WebConst.NEW)) {
                writer.println("<form action='AdminOrderServlet?action=create' method='POST'>");
                writer.println("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
                writer.println("<tr>");
                writer.println("<td>Member Id:</td>");
                writer.println("<td>" + Common.createComboBox("cbMember", "cbMember", (String[])(lstMemberData.toArray(new String[0])), "", "") + "</td>");
                writer.println("</tr>");
                writer.println("<td>Order Date:</td>");
                writer.println("<td>" + Common.createTextbox("txtOrderDate", "txtOrderDate", "", "", "") + "</td>");
                writer.println("</tr>");
                writer.println("<td>Payment:</td>");
                writer.println("<td>" + Common.createTextbox("txtPayment", "txtPayment", "" , "", "") + "</td>");
                writer.println("</tr>");
                writer.println("<td>Shipping:</td>");
                writer.println("<td>" + Common.createTextbox("txtShipping", "txtShipping", "", "", "") + "</td>");
                writer.println("</tr>");
                writer.println("<tr colspan='2'>");
                writer.println("<td>" + Common.createSubmitButton("btnCreate", "btnCreate", "Create", "") + "</td>");
                writer.println("</tr>");
                writer.println("</table>");
                writer.println("</form>");
            }

        } catch (Exception e) {
        }
        return SKIP_BODY;
    }
}
